package com.yisu.expressway.application;

import android.app.ActivityManager;
import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.baidu.mapapi.SDKInitializer;
import com.yisu.expressway.BuildConfig;
import com.yisu.expressway.login.ExInvalidUserListener;
import com.yisu.expressway.login.LoginManager;
import com.yisu.expressway.login.UserInvalidHandler;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.push.GetPushManager;
import com.yisu.expressway.utils.HBLog;
import com.yisu.expressway.utils.SocialUtils;
import com.youzan.sdk.YouzanSDK;
import java.util.List;

import com.iflytek.cloud.SpeechUtility;

import static com.yisu.expressway.settings.AppConstant.VOICE_APP_ID;

/**
 * Created by Administrator on 2016/9/14 0014.
 */
public class ExApplication extends MultiDexApplication {
    private static ExApplication sInstance;

    public static ExApplication getInstance() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {
            sInstance = this;
            HBLog.setIsLoggable(BuildConfig.isLoggable);
            ApiSetting.setmNetworkUrl(BuildConfig.EX_API);
            SocialUtils.init();
            ApiClientHelper.init(this);
            LoginManager.initialize(this);
            // 添加初始化代码，否则某些手机无法开启导航
            SDKInitializer.initialize(this);
            YouzanSDK.init(this, BuildConfig.YOUZAN_UA);
            GetPushManager.getInstance().init(getApplicationContext());
            SpeechUtility.createUtility(this, "appid=" + VOICE_APP_ID);//58329dcf
            UserInvalidHandler.setInvalidListener(new ExInvalidUserListener());
        }
    }

    /**
     * 获取应用本身进程
     * @param context
     * @return
     */
    public  String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    public boolean isRunOnBackGroud() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {

                    return true;
                }else{

                    return false;
                }
            }
        }
        return false;
    }

}
